/*
 * TierManagementSB.java
 */

package session.TX;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import entity.*;
import exception.ExistException;
import javax.persistence.Query;

@Stateful
public class TierManagementSB implements
        session.TX.TierManagementSBRemote {
    @PersistenceContext()
    EntityManager em;
    StakeholderTier stakeholderTier;
    Stakeholder stakeholder;

    public TierManagementSB() {}

    public void createTier(String name, Integer commissionPercent, Double feeSubscription, Double feeMarketIntelligence, Double costPerClick) throws ExistException {
        name = name.toLowerCase();
        if (!checkTierConflict(name)) {
        stakeholderTier = new StakeholderTier();
        stakeholderTier.create(name, commissionPercent, feeSubscription, feeMarketIntelligence, costPerClick);
        em.persist(stakeholderTier);
        } else {
            throw new ExistException("Tier name conflict: " + name);
        }
        }
    
    public boolean checkTierConflict (String name) {
        stakeholderTier = em.find(StakeholderTier.class, name);
        if (stakeholderTier == null) {
            return false;
        }
        else {                    
            return true;
        }
    }
    
    public boolean checkTierGotStakeholder (String name) {
        stakeholderTier = em.find(StakeholderTier.class, name);
        System.out.println("stakeholder: " + stakeholderTier == null);
        List<Stakeholder> stakeholders = getAllStakeholders();
        boolean check = false;
        for(int i = 0; i < stakeholders.size(); i++){
            if ((stakeholders.get(i).getStakeholderTier()).equals(stakeholderTier)) {
                check = true;
                break;
            }
        }
        return check;
    }
    
    public List<Stakeholder> getAllStakeholders() {
        Query q = em.createQuery("SELECT r FROM Stakeholder r");
        List<Stakeholder> result = new ArrayList<Stakeholder>();
        for(Object o: q.getResultList()){
            Stakeholder r = (Stakeholder)o;
                result.add(r);
    }
        return result;
    }


    public void deleteTier(String name) throws ExistException {
        stakeholderTier = em.find(StakeholderTier.class, name);
        if (!checkTierGotStakeholder(name)) {
            em.remove(stakeholderTier);
        } else {
            throw new ExistException("Tier name cannot be removed " + name);
        }
    }

    public List viewTier(String name) {
        List stateList = new ArrayList();
        stakeholderTier = em.find(StakeholderTier.class, name);
        stateList.add(stakeholderTier.getName());
        stateList.add(stakeholderTier.getCommissionPercent());
        stateList.add(stakeholderTier.getFeeSubscription());
        stateList.add(stakeholderTier.getFeeMarketIntelligence());
        stateList.add(stakeholderTier.getCostPerClick());
        return stateList;
    }
    
    public List<StakeholderTier> getAllTiers() {
        Query query = em.createQuery("SELECT e FROM StakeholderTier e");
        return query.getResultList();
    }
    
    public List<StakeholderTier> getSpecificTier(String tier) {
//        Query query = em.createQuery("SELECT OBJECT(e) FROM StakeholderTier e where e.name LOCATE(:name, e.name)>0");
        Query query = em.createQuery("SELECT OBJECT(e) FROM StakeholderTier e where e.name = :name");
        query.setParameter("name", tier);
        return query.getResultList();
    }

    public boolean modifyTier(String name, Integer commissionPercent, Double feeSubscription, Double feeMarketIntelligence, Double costPerClick) {
        System.out.println("TierManagement Session Bean: modifyTier()");
        stakeholderTier = em.find(StakeholderTier.class, name);
        if (stakeholderTier == null) {
            return false;
        }
        else {
        stakeholderTier.setCommissionPercent(commissionPercent);
        stakeholderTier.setFeeMarketIntelligence(feeMarketIntelligence);
        stakeholderTier.setFeeSubscription(feeSubscription);
        stakeholderTier.setCostPerClick(costPerClick);
        em.flush();
        return true;
        }
    }

    @Remove
    public void remove() {
        System.out.println("TierManagementBean:remove()");
    }

}